package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.request.basic.SetRootUserDataPermissionRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.UserDataPermissionResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.UserDataPermissionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户数据权限 前端控制器
 * @author yangdongjie
 * @LocationController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "用户数据权限")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class UserDataPermissionController {

	@Autowired
    private UserDataPermissionService userDataPermissionService;

    private static final String REQUEST_MODEL = "/userdatapermission";
    
    /**
     * 设置指定用户的数据权限
     * @param request
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "设置指定用户的数据权限",notes = "设置指定用户的数据权限")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/setUserDataPermission")
    public <T> Result<T> setUserDataPermission(
    		@RequestBody @Valid SetRootUserDataPermissionRequest request, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return userDataPermissionService.setUserDataPermission(request, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
	/**
	 * 查询指定用户的数据权限列表
	 * @param userId
	 * @param attr
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "查询指定用户的数据权限列表",notes = "查询指定用户的数据权限列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getPermsByUserIdAndAttr")
    public List<UserDataPermissionResponse> getPermsByUserIdAndAttr(
    		@ApiParam(value = "用户ID", required = true) 
    		@RequestParam Integer userId, 
    		@ApiParam(value = "OWN：雇主、FAC：工厂、WHS：仓库、CTC：成本中心、WKC：工作中心", required = true) 
    		@RequestParam String attr, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return userDataPermissionService.getPermsByUserIdAndAttr(userId, attr);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
